<?php defined('SYSPATH') or die('No direct script access.');

// Карта сайта
class Controller_Sitemap extends Controller_Base
{
    public function action_index()
    {
        // Подкатегории и статьи
        $articles = $this->category_model->get_full_categories();
        
        foreach($articles as &$category)
        {
            foreach($category['subcategories'] as &$subcategory)
            {
                $subcategory['articles'] = $this->article_model->get_all_published_articles($subcategory['subcategory_id']);
                
                foreach($subcategory['articles'] as &$article)
                {
                    $article['url'] = $this->main_model->transliterate($article['title']);   
                }
            }   
        }
        
        // Страницы
        $pages = $this->page_model->get_pages();
        
        // Компании
        $companies = $this->company_model->get_all_published_companies();
        
        // Картинки
        $images = $this->gallery_model->get_all_published_images();
        
        $base = URL::base('http');
        
        $sitemap = new Sitemap;

        foreach($articles as &$category)
        {
            foreach($category['subcategories'] as &$subcategory)
            {
                $url = new Sitemap_URL();
                $url->set_loc($base.$category['url'].'/'.$subcategory['url'])->set_last_mod(time())/*->set_change_frequency('always')->set_priority(1)*/;                
                $sitemap->add($url);
                
                foreach($subcategory['articles'] as $article)
                {
                    $url = new Sitemap_URL();
                    $url->set_loc($base.$article['article_id'].'-'.$this->main_model->transliterate($article['title']))->set_last_mod(time())/*->set_change_frequency('always')->set_priority(1)*/;                
                    $sitemap->add($url); 
                }
            }
        }
        
        foreach($pages as $page)
        {
            $url = new Sitemap_URL();
            $url->set_loc($base.'page/'.$page['url'])->set_last_mod(time())/*->set_change_frequency('always')->set_priority(1)*/;                
            $sitemap->add($url); 
        }
        
        foreach($images as &$image)
        {
            $url = new Sitemap_URL();
            $url->set_loc($base.'gallery/img-'.$image['image_id'])->set_last_mod(time())/*->set_change_frequency('always')->set_priority(1)*/;                
            $sitemap->add($url);    
        }
        
        foreach($companies as &$company)
        {
            $url = new Sitemap_URL();
            $url->set_loc($base.'companies/company-'.$company['company_id'])->set_last_mod(time())/*->set_change_frequency('always')->set_priority(1)*/;                
            $sitemap->add($url);    
        }

        $this->response->headers('Content-type', 'text/xml');
        $this->response->body($sitemap);
    }
    
    public function action_get_sitemap()
    {
        // Подкатегории и статьи
        $articles = $this->category_model->get_full_categories();
        
        foreach($articles as &$category)
        {
            foreach($category['subcategories'] as &$subcategory)
            {
                $subcategory['articles'] = $this->article_model->get_all_published_articles($subcategory['subcategory_id']);
                
                foreach($subcategory['articles'] as &$article)
                {
                    $article['url'] = $this->main_model->transliterate($article['title']);   
                }
            }   
        }
        
        // Страницы
        $pages = $this->page_model->get_pages();
        
        // Компании
        $companies = $this->company_model->get_all_published_companies();
        
        // Картинки
        $images = $this->gallery_model->get_all_published_images();
        
        $title   = 'Карта сайта';
        $content = new View('map_tpl');
        $content->articles  = $articles;
        $content->pages     = $pages;
        $content->companies = $companies;
        $content->images    = $images;
                
        $this->print_template($title, $content);
    }
} // End Sitemap
